package com.inphase.myfood.main;

import java.io.IOException;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HelloWorld {
	private HttpClient httpClient = null;
	private HttpGet httpGet = null;
	private HttpResponse httpResponse = null;
	private JSONObject json = null;
	public static final String MYKEY = "aMzlddU67x54K7huCVrRdtb5";
	public static String hostURL = "http://api.map.baidu.com/location/ip?"
			+ "ak=" + MYKEY;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		new HelloWorld().testConn();
	}

	/**
	 * 浏览器登陆 http://www.ip138.com/ 即可获取对外的真实IP地址
	 * 
	 * @return
	 */
	private static String getLocalIP() {
		return "171.88.173.167";
	}

	public void testConn() {
		// 组装URL
		hostURL += "&ip=" + getLocalIP();
		hostURL += "&coor=bd09ll";

		System.out.println(hostURL);
		httpClient = new DefaultHttpClient();
		httpGet = new HttpGet(hostURL);

		try {
			httpResponse = httpClient.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = httpResponse.getEntity();
				String resultStr = EntityUtils.toString(entity);
				json = JSONObject.fromObject(resultStr); 
				System.out.println(json.get("content"));
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
